<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/5/11
 * Time: 9:24
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class CommunityLikeModel
 * @package SDXApi\Model
 */
class CommunityLikeModel extends Model
{
    /**
     * @var string
     */
    protected $tableName = 'community_like';

    /**
     * Created by Jerry
     * @param int $id
     * @param int $uid
     * @return mixed
     */
    public function getCount($id = 0, $uid = 0)
    {
        $where = array(
            'community_id' => $id,
            'user_id' => $uid
        );

        return $this->where($where)->count();
    }

    /**
     * @author Jerry
     * @param int $userId
     * @return mixed
     */
    public function findCollectByUserId($userId = 0)
    {
        if (empty($userId)) {
            return false;
        }

        $where = array(
            'cl.user_id' => $userId,
            '_string'    => 'c.community_id is not null',
        );

        $field = 'c.community_id, c.title, c.file_url as banner_img, c.reading_times';

        $data = $this->alias('cl')
            ->field($field)
            ->join('LEFT JOIN __COMMUNITY__ AS c ON cl.community_id = c.community_id')
//            ->join('LEFT JOIN __COMMUNITY_TEMPLATE__ AS ct ON cl.community_id = ct.community_id AND ct.template_type = 1')
//            ->join('LEFT JOIN __COMMUNITY_CONTENT__ AS cc ON ct.id = cc.template_id')
            ->where($where)
            ->group('c.community_id')
            ->select();

        return $data;
    }

    /**
     * @author Jerry
     * @param int $communityId
     * @param int $userId
     * @return int
     */
    public function findCommunityLikeByIdAndUserId($communityId = 0, $userId = 0)
    {
        $where = array(
            'community_id'  =>  $communityId,
            'user_id'       =>  $userId,
        );

        $count = $this->where($where)->count();

        if ($count) {
            return 1;
        }

        return 0;
    }


}